#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h> 
#include <stdlib.h>
#include <stdint.h>

#include "cgic.h"
#include "../include/sem.h"


int cgiMain()
{
	key_t key;
	int semid;
	int shmid;
	struct env_info *shm_buf = NULL;

	struct env_info env_tmp;

	if((key = ftok("/app", 'i')) < 0)
	{
		perror("ftok");
		exit(1);
	}
        
    if((shmid = shmget(key, N, IPC_CREAT | IPC_EXCL | 0666)) < 0)
    {
        if(errno == EEXIST)
        {
            shmid = shmget(key,N,0666); 
            shm_buf = (struct env_info *)shmat (shmid,NULL,0);
        }
        else{
            perror("shmget");
            exit(-1);
        }
    }


    if((shm_buf = (struct env_info*)shmat(shmid, NULL, 0)) == (void*)-1)
    {
        perror("shmat");
        exit(1);
    }
    if((semid  = semget(key, 1, IPC_CREAT | IPC_EXCL | 0666)) < 0)  //信号灯，用于互斥操作 NOTE:与共享内存用的一个键值
    {
        if(errno == EEXIST)
        {
            semid = semget(key,1,0666);
            printf("sem_init\n");

        }
        else
        {
            perror("semget");
            exit(-1);
        }   
    }
    else
    {
        union semun myun;
        myun.val = 1;
        if(semctl(semid, 0, SETVAL, myun) < 0)
        {
            exit(1);
        }
    }


    sem_p(semid,0); //申请资源
    env_tmp = *shm_buf;  //取共享内存里的环境信息
    sem_v (semid, 0);//释放资源

    cgiHeaderContentType("text/html");  //设定输出数据的MIME类型
    fprintf(cgiOut, "<HTML><HEAD>"); 
    fprintf(cgiOut, "<TITLE>当前实时数据</TITLE>");  
    fprintf(cgiOut, "<style type=\"text/css\"> body,td,th {font-size: 12px;margin-left: 0px;margin-top: 0px;}</style></HEAD>"); 
    fprintf(cgiOut, "<BODY BGCOLOR=\"#F8F9FA\">"); 

    fprintf(cgiOut, "<meta http-equiv=\"refresh\" content=\"1\">");

    fprintf(cgiOut, "<table width=\"100%%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">");
    // 实时数据表头
    fprintf(cgiOut, 	"<tr bgcolor=\"#DEE4E6\">");

    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">名称</td>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">最新数值</td>");
    fprintf(cgiOut, 	"</tr>");

    // 温度采集	
    fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境温度</td>");
    fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d℃</td>", env_tmp.temp[0]);
    fprintf(cgiOut, 	"</tr>");

    // 湿度采集
    fprintf(cgiOut, 	"<tr>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境湿度</td>");
    fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d％</td>", env_tmp.hum[0]);
    fprintf(cgiOut, 	"</tr>");

    // 光照采集
    fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境光照</td>");
    fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%dlux</td>", env_tmp.ill);
    fprintf(cgiOut, 	"</tr>");

    // 电量采集
    fprintf(cgiOut, 	"<tr>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">电池电量</td>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", env_tmp.bet);
    fprintf(cgiOut, 	"</tr>");

    // ADC采集	
    fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">模拟电压</td>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", env_tmp.adc);
    fprintf(cgiOut, 	"</tr>");

    fprintf(cgiOut, 	"<tr>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">设备姿态</td>");
    fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\"><table width=\"200\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");
    fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
    fprintf(cgiOut, 					"<td width=\"25%%\">&nbsp;</td>");
    fprintf(cgiOut, 					"<td width=\"25%%\">X</td>");
    fprintf(cgiOut, 					"<td width=\"25%%\">Y</td>");
    fprintf(cgiOut, 					"<td width=\"25%%\">Z</td>");
    fprintf(cgiOut, 			"</tr>");
    fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
    fprintf(cgiOut, 					"<td>原始值</td>");
    fprintf(cgiOut, 					"<td>%d</td>", env_tmp.x);
    fprintf(cgiOut, 					"<td>%d</td>", env_tmp.y);
    fprintf(cgiOut, 					"<td>%d</td>", env_tmp.z);
    fprintf(cgiOut, 			"</tr>");
    fprintf(cgiOut, 		"</table></td>");
    fprintf(cgiOut, 	"</tr>");
    fprintf(cgiOut, "</table>");

    fprintf(cgiOut, "<script type=\"text/jscript\">");
    fprintf(cgiOut, "setTimeout(\"self.close()\", 3000)");
    fprintf(cgiOut, "</script>");
    fprintf(cgiOut, "</BODY>"); 
    fprintf(cgiOut, "</HTML>");
    fflush(stdout);



    return 0;
}


